package com.deryun.tyl.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.deryun.tyl.bean.Contact;
import com.deryun.tyl.bean.ContentMessage;
import com.deryun.tyl.bean.News;
import com.deryun.tyl.bean.Platform;
import com.deryun.tyl.bean.Policy;
import com.deryun.tyl.bean.Publicity;
import com.deryun.tyl.bean.Purchasing;
import com.deryun.tyl.bean.Standard;
import com.deryun.tyl.bean.User;
import com.deryun.tyl.bean.tempBean.FriendConnect;
import com.deryun.tyl.bean.tempBean.MyPublish;
import com.deryun.tyl.dao.impl.PlatformDaoImpl;
import com.deryun.tyl.dao.impl.PublicityDaoImpl;
import com.deryun.tyl.service.impl.ContactServiceImpl;
import com.deryun.tyl.service.impl.ContentMessageServiceImpl;
import com.deryun.tyl.service.impl.NewsServiceImpl;
import com.deryun.tyl.service.impl.PlatformServiceImpl;
import com.deryun.tyl.service.impl.PolicyServiceImpl;
import com.deryun.tyl.service.impl.PurchasingServiceImpl;
import com.deryun.tyl.service.impl.StandardServiceImpl;
import com.deryun.tyl.service.impl.UserServiceImpl;

@Controller
@RequestMapping("ftoac")
public class FirstToAdminController 
{
	
	@Resource(name="policyServiceImpl")
	private PolicyServiceImpl policyServiceImpl;
	
	@Resource(name="standardServiceImpl")
	private StandardServiceImpl standardServiceImpl;
	
	@Resource(name="purchasingServiceImpl")
	private PurchasingServiceImpl purchasingServiceImpl;
	
	@Resource(name="publicityDaoImpl")
	private PublicityDaoImpl publicityDaoImpl;
	
	@Resource(name = "newsServiceImpl")
	private NewsServiceImpl newsServiceImpl;
	
	@Resource(name="platformServiceImpl")
	private PlatformServiceImpl platformServiceImpl;
	
	@Resource(name="contactServiceImpl")
	private ContactServiceImpl contactServiceImpl;
	
	@Resource(name="contentMessageServiceImpl")
	private ContentMessageServiceImpl contentMessageSI;
	
	@Resource(name="userService")
	private UserServiceImpl userSI;
	
	@Resource(name="platformDaoImpl")
	private PlatformDaoImpl platformDaoImpl;
	
	
	@RequestMapping("/homepage")
	public String homePage(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/homepage";
	}
	
	//政策法规home页
	@RequestMapping("/loadZcfg")
	public String loadZcfg(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (user.getRole().equals("D")) {
			List<Policy> massage = policyServiceImpl.getAllPolicy();
			request.setAttribute("massage", massage);
		}else {
			List<Policy> massage = policyServiceImpl.getUserPolicy(user.getId().toString());
			request.setAttribute("massage", massage);
		}
		
		return "back_admin_manger/zcfg_home";
	}
	
	//添加政策法规
	@RequestMapping("/addContent")
	public String toAddZcfg(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/content_add";
	}
	//添加行业动态
	@RequestMapping("/toAddHydt")
	public String toAddHydt(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/hydt_add";
	}
	
	//行业动态主页
	@RequestMapping("/toLoadHydt")
	public String toLoadHydt(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/hydt_home";
	}
	
	//添加专题新闻
	@RequestMapping("/toAddZtxw")
	public String toAddZtxw(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/ztxw_add";
	}
	
	//专题新闻主页
	@RequestMapping("/toLoadZtxw")
	public String toLoadZtxw(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<News> massage = newsServiceImpl.getAllNews();
		request.setAttribute("massage", massage);
		return "back_admin_manger/ztxw_home";
	}
	
	//添加标准规范
	@RequestMapping("/toAddBzgf")
	public String toAddBzgf(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/bzgf_add";
	}
	//标准规范主页
	@RequestMapping("/toLoadBzgf")
	public String toLoadBzgf(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (user.getRole().equals("D")) {
			List<Standard> massage = standardServiceImpl.getAllStandard();
			request.setAttribute("massage", massage);
		}else {
			List<Standard> massage = standardServiceImpl.getUserStandard(user.getId().toString());
			request.setAttribute("massage", massage);
		}
		return "back_admin_manger/bzgf_home";
	}
	
	//添加采购资讯
	@RequestMapping("/toAddCgzx")
	public String toAddCgzx(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/cgzx_add";
	}
	
	//采购资讯主页
	@RequestMapping("/toLoadCgzx")
	public String toLoadCgzx(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (user.getRole().equals("D")) {
			List<Purchasing> massage = purchasingServiceImpl.getAllPurchasing();
			request.setAttribute("massage", massage);
		}else {
			List<Purchasing> massage = purchasingServiceImpl.getUserPurchasing(user.getId().toString());
			request.setAttribute("massage", massage);
		}
		return "back_admin_manger/cgzx_home";
	}
	
	//添加信息公开
	@RequestMapping("/toAddXxgk")
	public String toAddXxgk(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/xxgk_add";
	}
	
	//信息公开主页
	@RequestMapping("/toLoadXxgk")
	public String toLoadXxgk(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (user.getRole().equals("D")) {
			List<Publicity> massage = publicityDaoImpl.getAllPublicity();
			request.setAttribute("massage", massage);
		}else {
			List<Publicity> massage = publicityDaoImpl.getUserPublicity(user.getId().toString());
			request.setAttribute("massage", massage);
		}
		return "back_admin_manger/xxgk_home";
	}
	
	//添加社区服务供应商
	@RequestMapping("/toAddSqfwgys")
	public String toAddSqfwgys(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/sqfwgys_add";
	}
		

	
	//修改联系方式主页电话
	@RequestMapping("/toAddLxfs")
	public String toAddLxfs(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/lxfs_add";
	}
	
	//修改平台电话
	@RequestMapping("/toSetPtdh")
	public String toSetPtdh(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		Platform massage = platformServiceImpl.getPlatform();
		request.setAttribute("massage", massage);
		return "back_admin_manger/ptdh_set";
	}
		
	//联系方式主页
	@RequestMapping("/toLoadLxfs")
	public String toLoadLxfs(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<Contact> massage = contactServiceImpl.getAllContact();
		request.setAttribute("massage", massage);
		return "back_admin_manger/lxfs_home";
	}
	
	//添加产品/服务供应商
	@RequestMapping("/toAddCpfwgys")
	public String toAddCpfwgys(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/cpfwgys_add";
	}
		

	//产品/服务供应商排名
	@RequestMapping("/toSetRank")
	public String toSetRank(HttpServletRequest request)
	{
		
		return "back_admin_manger/cpfwgys_setRank";
	}
	@RequestMapping("/toAddPic")
	public String toAddPic(HttpServletRequest request){
		return "back_admin_manger/ztxw_addMutiPic";
	}
	
	private File upload; // 文件  
    private String uploadContentType; // 文件类型  
    private String uploadFileName; // 文件名  
	@RequestMapping("uploadFile")
	public void uploadFile(@RequestParam("upload") MultipartFile multipartFile,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=UTF-8");
//		response.setHeader("X-Frame-Options", "SAMEORIGIN");
		String CKEditorFuncNum = request.getParameter("CKEditorFuncNum");
		PrintWriter out;
		String s = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction("+CKEditorFuncNum+", '"+multipartFile.getOriginalFilename()+"');</script>";
		try {
			out = response.getWriter();
			out.print(s);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//添加产品页面
	@RequestMapping("toAddProduct")
	public String toAddProduct(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/product_add";
	}
	
	//项目发布页面
	@RequestMapping("toAddProject")
	public String toAddProject(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/project_add";
	}
	//活动发布页面
	@RequestMapping("toAddActivity")
	public String toAddActivity(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/activity_add";
	}
	
	//发布追踪列表页
	@RequestMapping("loadTrace")
	public String loadTrace(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<ContentMessage> message = contentMessageSI.getAll(user.getId().toString());
		request.setAttribute("message", message);
		return "back_admin_manger/trace_publish";
	}
	
	//我的发布首页
	@RequestMapping("toMyPublish")
	public String toMyPublish(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/myPublish";
	}
	
	//后台账号认证页面
	@RequestMapping("toAuthAccount")
	public String toAuthAccount(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/auth_account";
	}
	
	//后台机构认证页面
	@RequestMapping("toAuthInstitution")
	public String toAuthInstitution(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/auth_institution";
	}
	
	//后台一键通认证页面
	@RequestMapping("toAuthOneKey")
	public String toAuthOneKey(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/auth_oneKey";
	}
	
	//后台银行认证页面
	@RequestMapping("toAuthBank")
	public String toAuthBank(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/auth_bank";
	}
	
	//修改页面
	@RequestMapping("toModifyCM")
	public String toModifyCM(int id,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		ContentMessage message = contentMessageSI.getCM(id);
		request.setAttribute("message", message);
		if (message.getType().equals("1")){//内容
			return "back_admin_manger/content_modify";
		}
		if (message.getType().equals("2")){//产品
			return "back_admin_manger/product_modify";	
		}
		if (message.getType().equals("3")){//项目
			return "back_admin_manger/project_modify";
		}
		if (message.getType().equals("4")){//活动
			return "back_admin_manger/activity_modify";
		}
		return "";
	}
	
	//admin发布首页
	@RequestMapping("/toAdminPublish")
	public String toAdminAuth(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		MyPublish addCount = contentMessageSI.getAdminCount();
		request.getSession().setAttribute("allCount", addCount);
		return "back_admin_manger/admin_publish";
	}
	
	//admin审核首页
	@RequestMapping("/toAdminAuth")
	public String toAdminPublish(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		MyPublish addCount = contentMessageSI.getAdminAuthCount();
		request.getSession().setAttribute("authCount", addCount);
		return "back_admin_manger/admin_auth";
	}
	
	//admin数据库首页
	@RequestMapping("/toAdminDataBase")
	public String toAdminDataBase(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}

		return "back_admin_manger/admin_dataBase";
	}
	
	//admin审核内容/产品
	@RequestMapping("/toContentMessageAuth")
	public String toContentMessageAuth(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<ContentMessage> message = contentMessageSI.getAdminAuthCM();
		request.getSession().setAttribute("authCM", message);
		return "back_admin_manger/content_message_auth";
	}
	
	//admin审核账号
	@RequestMapping("/toAccountAuth")
	public String toAccountAuth(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<User> message = userSI.getAllUser();
		request.getSession().setAttribute("authAccount", message);
		return "back_admin_manger/account_auth";
	}
	
	//admin内容库
	@RequestMapping("/toCMBase")
	public String toCMBase(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<ContentMessage> message = contentMessageSI.getAdminAll();
		request.setAttribute("message", message);
		return "back_admin_manger/content_base";
	}
	
	//admin内容库
	@RequestMapping("/toProductBase")
	public String toProductBase(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<ContentMessage> message = contentMessageSI.getAdminAll();
		request.setAttribute("message", message);
		return "back_admin_manger/product_base";
	}
	
	//admin内容库
	@RequestMapping("/toProjectBase")
	public String toProjectBase(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<ContentMessage> message = contentMessageSI.getAdminAll();
		request.setAttribute("message", message);
		return "back_admin_manger/project_base";
	}
	
	//admin机构库
	@RequestMapping("/toInstitutionBase")
	public String toInstitutionBase(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<User> message = userSI.getAllUser();
		request.setAttribute("message", message);
		return "back_admin_manger/institution_base";
	}
	
	//admin认证库
	@RequestMapping("/toAuthBase")
	public String toAuthBase(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<User> message = userSI.getAllUser();
		request.setAttribute("message", message);
		return "back_admin_manger/auth_base";
	}
	
	//首页图片管理页面
	@RequestMapping("/toIndexImage")
	public String toIndexImage(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/index_image";
	}
	
	//首页友情链接
	@RequestMapping("/toFriendConnect")
	public String toFriendConnect(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) 
		{
			return "redirect:/civil/homepage";
		}
		List<FriendConnect> friendConnects = contentMessageSI.getFC();
		request.setAttribute("message", friendConnects);
		request.setAttribute("result", request.getAttribute("result"));
		return "back_admin_manger/index_friend_connect";
	}
	
	//设置上首页的机构页面
	@RequestMapping("/toSupplierToFront")
	public String toSupplierToFront(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		List<User> users = userSI.getAllUser();
		request.setAttribute("message", users);
		return "back_admin_manger/supplier_to_front";
	}
	
	//管理平台电话
	@RequestMapping("/toPlatform")
	public String toPlatform(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		Platform platform = platformDaoImpl.getPlatform();
		request.setAttribute("message", platform);
		String res = (String) request.getAttribute("about");
		request.setAttribute("result", res);
		return "back_admin_manger/platform";
	}
}
